package com.dsjy.yf.address.config.globalprocess;

import com.dsjy.yf.address.annotation.FocusAnnotation;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @author whj
 * @version 1.0
 * @date 2020-12-02 14:28
 */
@RestControllerAdvice
public class ResultResponseAdvice implements ResponseBodyAdvice {
    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        FocusAnnotation methodAnnotation = null;
        methodAnnotation = methodParameter.getDeclaringClass().getAnnotation(FocusAnnotation.class);
        if (methodAnnotation == null) {
            methodAnnotation = methodParameter.getMethodAnnotation(FocusAnnotation.class);
        }
        return methodAnnotation != null;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        //TODO 处理下文件流问题
        if (o instanceof Result) {
            return o;
        }
        return new Result(o);
    }
}
